package com.atguigu.gmall.realtime.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author caodan
 * @version 1.0
 * @Deacription Phoenix 工具类
 * @date 2024-01-23 10:00
 */
public class PhoenixUtil {

    private PhoenixUtil(){
        throw new RuntimeException("DO NOT INSTANCE");
    }
    /**
     * 执行出入的Phoenix SQL语句
     * @param sql 执行的SQL
     * @param conn 连接对象
     */
    public static void executeSQL(String sql, Connection conn) {
        PreparedStatement ps = null;
        try {
            //获取数据库操作对象
            ps = conn.prepareStatement(sql);
            //执行SQL语句
            ps.execute();
        } catch (Exception e) {
            System.out.println("执行操作phoenix语句发生了异常");
        } finally {
            close(ps, conn);
        }
    }

    public static void close(PreparedStatement ps, Connection conn) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
